package com.hancher.plugins.hrnumberaddsub;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lxj.xpopup.core.CenterPopupView;

import java.math.BigDecimal;

/**
 * 基类PopupWindow
 *
 * @author xx
 * @Date 2017-1-18 上午11:10:05
 */
public class HrNumberKeyboardPopup2 extends CenterPopupView {
    TextView tv1;
    TextView tv2;
    TextView tv3;
    TextView tv4;
    TextView tv5;
    TextView tv6;
    TextView tv7;
    TextView tv8;
    TextView tv9;
    TextView tv0;
    TextView tv_sure;
    TextView tvPoint;
    LinearLayout flDelete;
    private Context mContext;
    private StringBuilder sbInput;
    private View mView;
    private OnViewClickListener onViewClickListener;
    private boolean isUpdate;
    private int scale;
    private int maxLength;
    private String defaultInputNum;
    private static final String TAG = "HrNumberKeyboardPopup2";

    /**
     * @param context
     */
    public HrNumberKeyboardPopup2(Context context, OnViewClickListener onViewClickListener) {
        super(context);
        mContext = context;
        this.onViewClickListener = onViewClickListener;
        this.scale = 2;
        this.defaultInputNum = null;
        this.maxLength = 6;
    }
    /**
     * @param context
     */
    public HrNumberKeyboardPopup2(Context context, OnViewClickListener onViewClickListener, int scale, String defaultInputNum, int maxLength) {
        super(context);
        mContext = context;
        this.onViewClickListener = onViewClickListener;
        this.scale = scale;
        this.defaultInputNum = defaultInputNum;
        this.maxLength = maxLength;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.layout_number_keyboard_edit;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        sbInput = new StringBuilder();
        tv0 = findViewById(R.id.tv_0);
        tv1 = findViewById(R.id.tv_1);
        tv2 = findViewById(R.id.tv_2);
        tv3 = findViewById(R.id.tv_3);
        tv4 = findViewById(R.id.tv_4);
        tv5 = findViewById(R.id.tv_5);
        tv6 = findViewById(R.id.tv_6);
        tv7 = findViewById(R.id.tv_7);
        tv8 = findViewById(R.id.tv_8);
        tv9 = findViewById(R.id.tv_9);
        tv_sure = findViewById(R.id.tv_sure);
        tvPoint = findViewById(R.id.tv_point);
        flDelete = findViewById(R.id.fl_delete);
        mView = findViewById(R.id.tv_num);
        tv0.setOnClickListener(v -> inputNumber(tv0.getText().toString()));
        tv1.setOnClickListener(v -> inputNumber(tv1.getText().toString()));
        tv2.setOnClickListener(v -> inputNumber(tv2.getText().toString()));
        tv3.setOnClickListener(v -> inputNumber(tv3.getText().toString()));
        tv4.setOnClickListener(v -> inputNumber(tv4.getText().toString()));
        tv5.setOnClickListener(v -> inputNumber(tv5.getText().toString()));
        tv6.setOnClickListener(v -> inputNumber(tv6.getText().toString()));
        tv7.setOnClickListener(v -> inputNumber(tv7.getText().toString()));
        tv8.setOnClickListener(v -> inputNumber(tv8.getText().toString()));
        tv9.setOnClickListener(v -> inputNumber(tv9.getText().toString()));
        tvPoint.setOnClickListener(v -> inputNumber(tvPoint.getText().toString()));
        flDelete.setOnClickListener(v -> {
            isUpdate = true;
            // 删除键
            if (sbInput.length() > 0) {
                sbInput.deleteCharAt(sbInput.length() - 1);
            }
            if (mView != null && mView instanceof TextView) {
                ((TextView) mView).setText(sbInput.toString());
            } else if (onViewClickListener != null) {
                onViewClickListener.afterNumberChanged(sbInput.toString());
            }
        });
        tv_sure.setOnClickListener((view) -> {
            if (isUpdate && onViewClickListener != null) {
                if (sbInput.length() <= 0) {
                    onViewClickListener.onComplete(0);
                } else {
                    onViewClickListener.onComplete(Double.parseDouble(sbInput.toString()));
                }
            }
            // 清空输入内容
            sbInput.setLength(0);
            isUpdate = false;
            dismiss();
        });
        if (!TextUtils.isEmpty(defaultInputNum)) {
            try {
                BigDecimal bigDecimal = new BigDecimal(defaultInputNum);
                sbInput.append(defaultInputNum);
            } catch (Exception e) {
                Log.d(TAG, "onCreate: defaultInputNum format fail");
            }
        }
    }

    private int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 点击数字和小数点
     *
     * @param number 输入内容
     */
    private void inputNumber(String number) {
        if (sbInput.indexOf(".") >= 0) {
            // 禁止小数点输入多次
            if (".".equals(number)) {
                return;
            }
            // 小数点后输入2位
            if (sbInput.toString().split("\\.").length > 1
                    && sbInput.toString().split("\\.")[1].length() >= scale) {
                return;
            }
        } else {
            if (sbInput.length() > maxLength) {
                return;
            } else if (sbInput.length() == maxLength) {
                if (!number.equals(".")) {
                    return;
                }
            }
        }
        isUpdate = true;
        if (sbInput.length() == 0) {
            if (".".equals(number)) {
                sbInput.append("0.");
            } else {
                sbInput.append(number);
            }
        } else if ("0".equals(sbInput.toString())) {
            if (!".".equals(number)) {
                sbInput.setLength(0);
            }
            sbInput.append(number);
        } else {
            sbInput.append(number);
        }
        if (mView != null && mView instanceof TextView) {
            ((TextView) mView).setText(sbInput.toString());
        } else if (onViewClickListener != null) {
            onViewClickListener.afterNumberChanged(sbInput.toString());
        }
    }

    public interface OnViewClickListener {
        void afterNumberChanged(String s);

        void onComplete(double lastNum);
    }
}
